/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package orest.common.dao;
import java.util.*;
import java.sql.*;
import orest.common.response.Response;
import orest.common.valueobject.KjcgNeedProcessVal;
import orest.common.util.DataBase;
import orest.common.util.Tools;
/**
 *
 * @author admin
 */
public class KjcgNeedProcessDao {
    
   public KjcgNeedProcessDao() {
    }
    
  
    public boolean addKjcgNeedProcess(KjcgNeedProcessVal obj){
    	boolean result = true;
        PreparedStatement pstmt = null;
        Connection conn = null;
        DataBase db = new DataBase();
        Tools tool = new Tools();
        String sql = "";
        try{
                   sql = " insert into T_KjcgNeedProcess(ID, NeedID, Content," +
                         " UserID,SubmitDate)";
                  sql += " values('" + 
                          obj.getID() + "', '" +
                          obj.getNeedID() +"', '" +
                          obj.getContent() + "', '" +
                          obj.getUserID()+"','"+
                          tool.gettimestamp()+"')";
                                               
            conn = db.getSysConnectionPool();
            pstmt = conn.prepareStatement (sql);
            pstmt.execute();
            return result;
        }
        catch(SQLException e){
            e.printStackTrace();
            result = false;
            return result;
        }
        catch(Exception e){
            e.printStackTrace();
            result = false;
            return result;
        }
        finally{
            /* close the statement */
            if(null != pstmt){
                try{
                    pstmt.close ();
                    pstmt = null;
                }
                catch(SQLException e){
                	e.printStackTrace();
                }
            }
            
            /* release the conn */
            if(null != conn){
                try{
                    conn.close();
                    conn = null;
                    db = null;
                }
                catch(Exception e){
                	e.printStackTrace();
                }
            }
        }
    } 



   

  

     public boolean findKjcgNeedProcessByNeedID(Response rp, String id){
        boolean result = true;
        ArrayList al = new ArrayList();

        /* dao object begin */
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn = null;
        DataBase db = new DataBase();
        /* dao object end */

        /* sql string begin */
        String sql = "select * from T_KjcgNeedProcess where  NeedID = ?";
        /* sql string end */

        /*
           get all of value of the production.
         */
        try{
           // conn = db.getMyConnection ();
            conn = db.getSysConnectionPool();
            pstmt = conn.prepareStatement (sql);
            pstmt.setString(1, id);
            rs = pstmt.executeQuery ();
            while(rs.next ()){
                 al.add (new KjcgNeedProcessVal(
                    rs.getString("ID"),
                    rs.getString("NeedID"),
                    rs.getString("Content"),
                    rs.getString("UserID"),
                    rs.getDate("SubmitDate")
                 
                    ));
            }
            rp.setValObjs (al);
            return result;
        }
        catch(SQLException e){
            System.out.println ("When get KjcgNeedProcess info," +
                    " there is a exception occured!! ");
            System.out.println ("The class is KjcgNeedProcessDao! " +
                                 "The function is findKjcgNeedProcessByPrk! " +
                                 e);
            result = false;
            return result;
        }
        catch(Exception e){
            System.out.println ("When call the getConnection, have a"+
                                 "exception occured!   ");
            System.out.println ("The class is KjcgNeedProcessDao and the " +
                                 "function is findKjcgNeedProcessByPrk! " + e);
            result = false;
            return result;
        }
        finally{
            /* close the prestatement */
            if(null != pstmt){
                try{
                    pstmt.close ();
                    pstmt = null;
                }
                catch(SQLException e){
                    System.out.println ("In the class of KjcgNeedProcessDao " +
                            "and the function is findKjcgNeedProcessByPrk, when" +
                            " close the pstmp, happen a new exception! " + e);
                }
            }

            /* close the rs */
            if(null != rs){
                try{
                    rs.close ();
                    rs = null;
                }
                catch(SQLException e){
                    System.out.println ("In the class of KjcgNeedProcessDao and " +
                            "the function is findKjcgNeedProcessByPrk, when" +
                            " close the rs, happen a new exception! " + e);
                }
            }

            /* release the conn */
            if(null != conn){
                try{
                    conn.close();
                    conn = null;
                    db = null;
                }
                catch(Exception e){
                    System.out.println ("In the class of KjcgNeedProcessDao and" +
                            "the function is findKjcgNeedProcessByPrk, when " +
                            " close the conn, happen a new exception! " + e);
                }
            }
        }
    }
 



    

      

   

      public boolean deleteKjcgNeedProcess(String id) {
    	boolean result = true;
        PreparedStatement pstmt = null;
        Connection conn = null;
        DataBase db = new DataBase();

        String sql = "delete from  T_KjcgNeedProcess where NeedID = '" + id + "'";

        try{
            conn = db.getSysConnectionPool();
            pstmt = conn.prepareStatement (sql);
            pstmt.execute();
            return result;
        }
        catch(SQLException e){
            e.printStackTrace();
            result = false;
            return result;
        }
        catch(Exception e){
        	e.printStackTrace();
            result = false;
            return result;
        }
        finally{
            /* close the statement */
            if(null != pstmt){
                try{
                    pstmt.close ();
                    pstmt = null;
                }
                catch(SQLException e){
                	e.printStackTrace();
                }
            }

            /* release the conn */
            if(null != conn){
                try{
                    conn.close();
                    conn = null;
                    db = null;
                }
                catch(Exception e){
                	e.printStackTrace();
                }
            }
        }
    }

         public boolean updateKjcgNeedProcess(KjcgNeedProcessVal obj, String id){
    	boolean result = true;
        PreparedStatement pstmt = null;
        Connection conn = null;
        DataBase db = new DataBase();
        Tools tool = new Tools();
        String sql = "";
        try{
                   sql = " update  T_KjcgNeedProcess set ";
                  sql += "Content = '" + obj.getContent() +"', " +
                         "UserID = '" + obj.getUserID() + "'  ";
                  sql += "where ID = '" + id + "'";
            conn = db.getSysConnectionPool();
            pstmt = conn.prepareStatement (sql);
            pstmt.execute();
            return result;
        }
        catch(SQLException e){
            e.printStackTrace();
            result = false;
            return result;
        }
        catch(Exception e){
            e.printStackTrace();
            result = false;
            return result;
        }
        finally{
            /* close the statement */
            if(null != pstmt){
                try{
                    pstmt.close ();
                    pstmt = null;
                }
                catch(SQLException e){
                	e.printStackTrace();
                }
            }

            /* release the conn */
            if(null != conn){
                try{
                    conn.close();
                    conn = null;
                    db = null;
                }
                catch(Exception e){
                	e.printStackTrace();
                }
            }
        }
    }

}
